package com.youlu.campus.admin.ca.controller;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.exception.BusinessException;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.ShippingBatch;
import com.youlu.campus.entity.UserCaInfo;
import com.youlu.campus.entity.UserCaInfoComment;
import com.youlu.campus.entity.VO.req.CreateCaOrderTagVO;
import com.youlu.campus.entity.VO.req.UserCaInfoQueryVO;
import com.youlu.campus.service.ca.CaInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 用户证书订单
 */
@Slf4j
@RestController
public class UserCaController {
    @Autowired
    private CaInfoService caInfoService;

    @PostMapping("/ca-order/list")
    public RestResult list(@RequestBody @Valid UserCaInfoQueryVO req) {
        String comId = AuthUtils.getComId();
        req.setTenentId(comId);
        Page<UserCaInfo> users = caInfoService.list(req);
        if (users == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            users = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(users).build();
    }

    @PostMapping("/ca-order/find-one")
    public RestResult list(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        UserCaInfo userCaInfo = caInfoService.getUserCaInfo(comId, id);
        return RestResultBuilder.builder().success().data(userCaInfo).build();
    }

    @PostMapping("/ca-order/create-tag")
    public RestResult createTag(@RequestBody CreateCaOrderTagVO req) {
        String comId = AuthUtils.getComId();
        List<String> tags = caInfoService.createTags(req);
        return RestResultBuilder.builder().success().data(tags).build();
    }

    @PostMapping("/ca-order/create-comment")
    public RestResult createComment(@RequestBody UserCaInfoComment req) {
        String comId = AuthUtils.getComId();
//        req.setOptName(AuthUtils.getLoginName());
        boolean res = caInfoService.createComment(req);
        return RestResultBuilder.builder().success().build();
    }

    @PostMapping("/ca-order/comments")
    public RestResult getComment(@RequestBody UserCaInfoComment req) {
        String comId = AuthUtils.getComId();
        List<UserCaInfoComment> caInfos = caInfoService.getComments(req);
        return RestResultBuilder.builder().success(caInfos).build();
    }

    @PostMapping("/ca-order/shipping")
    public RestResult shipping(@RequestBody UserCaInfo req) {
        String comId = AuthUtils.getComId();
        boolean res = caInfoService.shipping(req);
        if (res) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca-order/pre-refund")
    public RestResult preRefund(@RequestBody UserCaInfo req) {
        String comId = AuthUtils.getComId();
        boolean res = caInfoService.preRefund(req);
        if (res) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca-order/do-refund")
    public RestResult doRefund(@RequestBody UserCaInfo req) {
        String comId = AuthUtils.getComId();
        boolean res = caInfoService.doRefund(req);
        if (res) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    /**
     * 订单导出
     *
     * @param
     * @param response
     */
    @PostMapping("/ca-order/export")
    public RestResult orderExport(@RequestBody UserCaInfoQueryVO req, HttpServletResponse response) {
        boolean result = caInfoService.export(req, response);
        log.info(":>>> 导出订单结果:{}", result);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca-order/shipping-batch/create")
    public RestResult createShippingBatch(@RequestBody ShippingBatch req) {
        boolean result = caInfoService.createShippingBatch(req);
        log.info(":>>> 创建发货批次结果:{}", result);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca-order/shipping-batch/list")
    public RestResult listShippingBatch(@RequestBody ShippingBatch req) {
        boolean result = caInfoService.createShippingBatch(req);
        log.info(":>>> 创建发货批次结果:{}", result);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/ca-order/shipping-batch/update")
    public RestResult updateShippingBatch(@RequestBody ShippingBatch req) {
        boolean result = caInfoService.updateShippingBatch(req);
        log.info(":>>> 修改发货批次结果:{}", result);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @GetMapping("/ca-order/shipping-batch/delete")
    public RestResult updateShippingBatch(@RequestParam("id") String id) {
        boolean result = caInfoService.deleteShippingBatch("", id);
        log.info(":>>> 删除发货批次结果:{}", result);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping(value = "/ca-order/shipping-batch/detail/import")
    @ResponseBody
    public RestResult importShippingBatchDetail(@RequestParam("file") MultipartFile file, @RequestParam("batchId") String batchId) {
        try {

            String fileName = file.getOriginalFilename();
            log.info(":>>> 上传文件为:{}", fileName);
            HashMap<String, Object> result = caInfoService.importShippingBatchDetail(file, batchId, "");
            log.info(":>>> 批量导入发货批次详情数据结果:{}", JSON.toJSON(result));
            return RestResultBuilder.builder().success(result).build();
        } catch (BusinessException e) {
            log.error(":>>> 批量导入图书价格错误:{}", e);
            throw e;
        } catch (Exception e) {
            log.error(":>>> 批量导入图书价格错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }
}
